<?php

namespace App\Http\Controllers\AdminShop;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use DB;

class GoodController extends Controller
{
    /**
     * 商品列表
     *
     */
    public function getList(Request $request)
    {
        // 读取用户数据
        $good = DB::table('goods')
                // ->join('goodspics','goods.id','=','goodspics.gid')
                ->join('shoptypes','goods.stid','=','shoptypes.id')
                ->where(function($query)use($request) {
                    $keyword = $request->input('keyword');
                    if(!empty($keyword)) {
                        $query->where('goodsname','like','%'.$keyword.'%');
                    }
                })
                ->where('goods.sid','=',session('pid'))
                ->select('goods.*','shoptypes.typename')
                ->paginate(5);

        return view('adminShop.good.list',[
            'good' => $good,
            'request' => $request
        ]);

    }

    /**
     * 商品添加
     */
    public function getAdd(Request $request)
    {
        $data = DB::table('shoptypes')->where('sid','=',session('pid'))->select('id','typename')->get();
        return view('adminShop.good.add',['data'=>$data]);
    }

    /**
     * 商品添加
     */
    public function postInsert(Request $request)
    {
        // 添加商品表单数据验证
        $this->validate($request,[
            'goodsname' => 'required',
            'price' => 'required',

        ],[
            'goodsname.required' => '商品名为必填',
            'price.required' => '价格必填',
        ]);

        $data = $request->except('_token','img');
        // $goodspic = [];

        // 处理文件上传操作
        if($request->hasFile('img')) {
            // 移动文件
            $name = time().rand(1000,9999);
            $suffix = $request->file('img')->getClientOriginalExtension();
            $dir = config('app.upload_dir');
            $request->file('img')->move($dir,$name.'.'.$suffix);
            $data['logo'] = trim($dir,'.').$name.'.'.$suffix;
        }

        // 事务处理
        // DB::beginTransaction();
        // $id = DB::table('goods')->insertGetId($data);
        $res = DB::table('goods')->insert($data);
        // 获取插入数据的id
        // $goodspic['gid'] = $id;
        // $res = DB::table('goodspics')->insert($goodspic);

        if($res) {
            // DB::commit();
            return redirect('/good/list')->with('ifno','商品添加成功');
        }else{
            // DB::rollback();
            return back()->with('info','商品添加失败');
        }

    }

    /**
     * 商品编辑修改页面
     */
    public function getEdit(Request $request)
    {
        // 读取用户数据
        $good = DB::table('goods')
                // ->join('goodspics','goods.id','=','goodspics.gid')
                ->join('shoptypes','goods.stid','=','shoptypes.id')
                ->where('goods.id','=',$request->id)
                ->select('goods.*','shoptypes.typename')
                ->first();

        $data = DB::table('shoptypes')->where('sid','=',session('pid'))->select('id','typename')->get();     
        return view('adminShop.good.edit',[
            'good'=>$good,
            'data'=>$data
        ]);
    }

    /**
     * 商品详情更新
     */
    public function postUpdate(Request $request)
    {
 

        // 添加商品表单数据验证
        $this->validate($request,[
            'goodsname' => 'required',
            'price' => 'required',

        ],[
            'goodsname.required' => '商品名为必填',
            'price.required' => '价格必填',
        ]);

        $data = $request->except('_token','img','id');
        // $goodspic = [];

        // 处理文件上传操作
        if($request->hasFile('img')) {
            // 移动文件
            $name = time().rand(1000,9999);
            $suffix = $request->file('img')->getClientOriginalExtension();
            $dir = config('app.upload_dir');
            $request->file('img')->move($dir,$name.'.'.$suffix);
            $data['logo'] = trim($dir,'.').$name.'.'.$suffix;
        }
            // 事务处理
            // DB::beginTransaction();
            $res1 = DB::table('goods')->where('id','=',$request->id)->update($data);

            // $res2 = DB::table('goodspics')->where('gid','=',$request->id)->update($goodspic);

            if($res1) {
                // DB::commit();
                return redirect('/good/list')->with('ifno','商品更新成功');
            }else{
                // DB::rollback();

                return back()->with('info','商品更新失败');
            }
        
        // else{
        //     $res3 = DB::table('goods')->where('id','=',$request->id)->update($data);
        //     if($res3) {
        //         return redirect('/good/list')->with('ifno','商品更新成功');
        //     }else{

        //         return back()->with('info','商品更新失败');
        //     }
        // }
    }

    /**
     * 商品删除
     */
    public function getDelete(Request $request)
    {
        
        $id = $request->input('id');
        $res = DB::table('goods')->where('id','=',$id)->first();
        $path = $res->logo;
        
        // 事务处理
        // DB::beginTransaction();
        // 执行删除
        $res1 = DB::table('goods')->where('id','=',$id)->delete();
        // $res2 = DB::table('goodspics')->where('goodspics.gid','=',$id)->delete();

        if($res1) {
            // DB::commit();
            if(!empty($path)) {
                @unlink('.'.$path);
            }            
            return back()->with('info','删除成功');
        }else{
            // DB::rollback();
            return back()->with('ifno','删除失败');
        }              

    }
}
